//
//  PictureBrowerAnimator.swift
//  JLWeibo
//
//  Created by sunjinliang on 15/12/19.
//  Copyright © 2015年 sunjinliang. All rights reserved.
//

import UIKit
protocol PictureBrowerPresentDeldegate : NSObjectProtocol{
    /// 获取跳转时的图片
    ///
    /// - parameter indexPath: 照片索引
    ///
    /// - returns: 返回跳转时的图片
    func imageViewFromePresent(indexPath: NSIndexPath)->UIImageView
    /// 获取跳转前图片的起始位置
    ///
    /// - parameter indexPath: 照片索引
    ///
    /// - returns: 返回照片相对于屏幕位置
    func PictureBrowerPresentFromRect(indexPath:NSIndexPath)->CGRect
    /// 获取跳转后图片的最终位置
    ///
    /// - parameter indexPath: 照片索引
    ///
    /// - returns: 返回照片相对于屏幕位置
    func PictureBrowerPresenttoRect(indexPath:NSIndexPath)->CGRect
}
protocol PictureBrowerDismissDeldegate : NSObjectProtocol{
    /// 获取图片浏览器退出时照片
    ///
    /// - returns: 返回照片
    func imageViewFromeDissmiss()->UIImageView
    /// 获取图片浏览器退出时图片的索引
    ///
    /// - returns: 返回索引
    func indexPathFromeDissmiss()->NSIndexPath
}
class PictureBrowerAnimator: NSObject,UIViewControllerTransitioningDelegate {
    /// 是否model的标记
    var isPresent : Bool = false
    weak var presentDelegate : PictureBrowerPresentDeldegate?
    weak var dismissDelegate : PictureBrowerDismissDeldegate?
    var indexPath : NSIndexPath?
    //提供动画的代理
    func setDelegateParam(presentDelegate : PictureBrowerPresentDeldegate,dismissDelegate : PictureBrowerDismissDeldegate, indexPath: NSIndexPath){
        self.presentDelegate = presentDelegate
        self.dismissDelegate = dismissDelegate
        self.indexPath = indexPath
    }
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = true
        return self
    }
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        isPresent = false
        return self
    }
}
extension PictureBrowerAnimator:UIViewControllerAnimatedTransitioning{
    /// 返回动画时长
    ///
    /// - parameter transitionContext: 转场动画的上下文 － 提供动画所需要的素材
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.5
    }
    /// 动画的具体实现
    ///
    /// - parameter transitionContext: 转场动画的上下文 － 提供动画所需要的素材
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        isPresent ? presentAnimation(transitionContext) : dismissAnimation(transitionContext)
    }
    /// 展现转场动画
    ///
    /// - parameter transitionContext: 转场动画的上下文 － 提供动画所需要的素材
    func presentAnimation(transitionContext: UIViewControllerContextTransitioning){
        guard let  presentDelegate = self.presentDelegate , indexPath = self.indexPath else{
            print("presentAnimation---出错")
            return
        }
        /// 获得要展现的视图
        let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
        //加到容器里
        transitionContext.containerView()?.addSubview(toView!)
        /// 设置透明度
        toView?.alpha = 0
        /// 获得用户点击的图片
        let image = presentDelegate.imageViewFromePresent(indexPath)
        //设置动画开始的位置
        image.frame = presentDelegate.PictureBrowerPresentFromRect(indexPath)
        //加到容器上
        transitionContext.containerView()?.addSubview(image)
        /// 执行动画
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            //设置动画结束时图片的位置
            image.frame = presentDelegate.PictureBrowerPresenttoRect(indexPath)
            image.alpha = 0
            toView?.alpha = 1.0
            }) { (_) -> Void in
                //删除执行动画的图片
                image.removeFromSuperview()
                //告诉系统完成动画
                transitionContext.completeTransition(true)
        }

    }
    /// 解除转场动画
    ///
    /// - parameter transitionContext: 转场动画的上下文 － 提供动画所需要的素材
    func dismissAnimation(transitionContext: UIViewControllerContextTransitioning){
        guard let presentDelegate = presentDelegate, dismissDelegate = dismissDelegate else {
            return
        }
        let fromeView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        let image = dismissDelegate.imageViewFromeDissmiss()
        let indexpathNew = dismissDelegate.indexPathFromeDissmiss()
        transitionContext.containerView()?.addSubview(image)
        fromeView?.hidden = true
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            fromeView?.alpha = 0
            image.frame = presentDelegate.PictureBrowerPresentFromRect(indexpathNew)
            }) { (_) -> Void in
                
                fromeView?.removeFromSuperview()
                fromeView?.hidden = false
                transitionContext.completeTransition(true)
        }
    }
}